package com.example.springtrans.controller;

import com.example.springtrans.service.LogService;
import com.example.springtrans.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping("/user2")
@RestController
public class UserController2 {
    @Autowired
    private UserService userService;
    @Autowired
    private LogService logService;
    @Transactional
    @RequestMapping("/register")
    public boolean register(String userName,String password){
        /**
         * 用户表和注册表的插入理应再Service完成
         * 此处为了方便，直接在Controller中完成
         */
        if(!StringUtils.hasLength(userName)||!StringUtils.hasLength(password)){
            return false;
        }
        Integer result = userService.insert(userName,password);
        System.out.println("result:"+result);
        //插入日志表
        Integer insert = logService.insert(userName, "用户注册");
        System.out.println("insert:"+insert);
        return true;
    }
}
